/**
 * @description 自定义路由拦截
 */

// import { isLength } from "html2canvas/dist/types/css/types/length";

// 白名单
const whiteList = [
  '/pages/login/register',
  '/pages/index/home',
  '/pages/login/agreement',
  '/pages/login/forgotPassword'
];
export default async function() {
  const list = ['navigateTo', 'redirectTo', 'switchTab'];
  list.forEach(item => {
    uni.addInterceptor(item, {
      invoke(e) {
		// 获取要跳转的页面路径（url去掉"?"和"?"后的参数）
		const url = e.url.split('?')[0];
		let hasLogin = false;
		try {
			hasLogin = uni.getStorageSync('isLogin');
		} catch (e) {
			// error
		}
		// 判断当前窗口是白名单，如果是则不重定向路由
		let pass = null;
		if (whiteList) {
			pass = whiteList.some((item) => {
			  return url === item
			})
		};
		console.log(pass,hasLogin)
		if(!hasLogin && !pass){
			uni.reLaunch({
				url:"/pages/login/login"
			});
			return false;
		}else{
			return e;
		}
      },
      fail(err) { // 失败回调拦截
        console.log(err);
      },
    });
  });
}